<?php
include "connect.php";
if (isset($_POST["dathang"])) {
    $row["gia"] = $_POST["gia"];
    $id = $_GET["id"];
    if (isset($_SESSION['user'])) {
        $user = $_SESSION ['user'];
        $query = "SELECT * FROM giohang WHERE id='$id' AND user='$user' AND tinhtrang='themgiohang'";
        $result = mysqli_query($conn, $query);
        $numrow = mysqli_num_rows($result);
        if ($numrow != 0) {
            echo "<script>alert('Sản phẩm này đã có trong giỏ hàng của Quý khách');</script>";
        } else {
            $ngaydat = date("Y-m-d");
            $query2 = "INSERT INTO giohang(id,user,soluong,tinhtrang,ngaydat) VALUES ('$id','$user',1,'themgiohang','$ngaydat')";
            $result2 = mysqli_query($conn, $query2) or die(mysqli_error());
            if ($result2)
                echo "<script>window.location='index.php?b=showcart';</script>";
            else
                echo "<script> alert('Có lỗi xảy ra trong quá trình mua hàng!');</script>";
        }
    }
}

$id = $_GET["id"];
$strSQL = "SELECT sanpham.*,loaisanpham.*,nhomsanpham.* from sanpham,loaisanpham,nhomsanpham where sanpham.id_loai=loaisanpham.id_loai "
        . "AND loaisanpham.id_nhom=nhomsanpham.id_nhom "
        . "AND sanpham.id='$id'";
$result = mysqli_query($conn, $strSQL);
$row = mysqli_fetch_array($result);
$gia2 = number_format($row["gia"], 0, '', '.');
if ($row["mota"] == "") {
    $description = 'Mô tả của sản phẩm này đang được cập nhật!';
} else {
    $description = $row["mota"];
}

$strSQL = "SELECT * FROM sanpham "
        . "WHERE id_loai={$row['id_loai']} "
        . "AND ( ghichu='hienthi' OR ghichu='new' ) "
        . "AND id<>'$id' "
        . "ORDER BY rand() LIMIT 0,36";
$result2 = mysqli_query($conn, $strSQL);
$relatedProducts = array();
while ($row2 = mysqli_fetch_array($result2)) {
    $relatedProducts[] = $row2;
}
?>
<div style="width:560px">
    <table width="760" border="0" cellspacing="0" cellpadding="0">
        <tr>
            <td height="33" width="448" style="background-color:#c91401; padding-left:30px; color:#FFF; font-size:16px; font-weight:bold">THÔNG TIN SẢN PHẨM</td> 
            <td width="112" height="33"><img src="images/bgtitle.jpg" width="112" height="33"></td>
        </tr>
    </table>
    <table width="760" border="0" cellspacing="0" cellpadding="0">
        <tr>
            <td colspan="3" height="40" style="border-bottom:1px solid #333; background:url(images/toplist-content.gif) repeat-x; font-weight:bold">
                <a href="index.php"><img src="images/Home.gif" width="16" height="16" border="0"></a>
                <img src="images/towred1-r.gif" width="16" height="9">
                <a href="?b=nhomsp&idn=<?php echo $row['id_nhom']; ?>"><?php echo $row['tennhom']; ?></a>      
                <img src="images/towred1-r.gif" width="16" height="9">
                <a href="?b=loaisp&idl=<?php echo $row["id_loai"]; ?>"><?php echo $row['tenloaisp']; ?></a> 
                <img src="images/towred1-r.gif" width="16" height="9">
                <a href="?b=chitiet&id=<?php echo $id ?>"><?php echo $row["tensp"]; ?></a>
            </td>
        </tr>
        <tr>
            <td width="220" rowspan="7" align="center" valign="top">
                <div onclick="var win = window.open('zoom.php?id=<?php echo $id; ?>', 'open_window', 'width=405, height=530, left=0, top=0')">
                    <p><img src="sanpham/small/<?php echo $row["hinh"]; ?>" width="170" height="170"><br />
                    <div class="button"> Xem ảnh lớn</div> <br />
                </div>
                <form method="post" name="form">
                    <input type="hidden" name="dathang" />
                    <input type="hidden" name="catid" value=<?php echo $id; ?>  /> 
                    <input type="hidden" name="gia" value="<?php echo $row["gia"]; ?>" />
                    <?php if (isset($_SESSION["user"])) { ?>
                        <a onClick="document.form.submit();">
                            <img src="images/chovaogiohang.jpg" width="151" height=21 border=0>
                        </a>
                    <?php } else { ?>
                        <a href=index.php?b=giohang&id=<?php echo $id; ?>&g=<?php echo $row['gia']; ?>><img src="images/chovaogiohang.jpg" width="151" height=21 border=0></a> 
                    <?php } ?>
                </form></td>
            <td width="120" height="25" style="padding-left:20px; border-bottom:1px dotted #666; border-right:1px dotted #666">Mã sản phẩm:</td>
            <td width="220" style="border-bottom:1px dotted #666; padding-left:5px"><span style="font-size:18px; color:#00F; font-weight:bold"><?php echo $row["tensp"]; ?></span></td>
        </tr>
        <tr>
            <td height="25" style="padding-left:20px; border-bottom:1px dotted #666; border-right:1px dotted #666">Giá:</td>
            <td style="border-bottom:1px dotted #666; padding-left:5px"><font color="#FF0000"><?php echo $gia2 . "VND"; ?></font></td>
        </tr>
        <tr>
            <td height="25" style="padding-left:20px; border-bottom:1px dotted #666; border-right:1px dotted #666">Thuế VAT:</td>
            <td style="border-bottom:1px dotted #666; padding-left:5px"><font color="#FF0000">Giá trên chưa bao gồm thuế</font></td>
        </tr>
        <tr>
            <td height="25" style="padding-left:20px; border-bottom:1px dotted #666; border-right:1px dotted #666">Bảo hành:</td>
            <td style="border-bottom:1px dotted #666; padding-left:5px">12 tháng.</td>
        </tr>
        <tr>
            <td height="40" style="padding-left:20px; border-bottom:1px dotted #666; border-right:1px dotted #666">Vận chuyển:</td>
            <td style="border-bottom:1px dotted #666; padding-left:5px">Giao hàng toàn quốc</td>
        </tr>
        <tr>
            <td height="40" style="padding-left:20px; border-bottom:1px dotted #666; border-right:1px dotted #666">Thời gian <br />
                giao hàng:</td>
            <td style="border-bottom:1px dotted #666; padding-left:5px" >
                <?php if ($row["trangthai"] == 0) { ?>
                    <strong>7 ngày</strong> sau khi đặt hàng
                <?php } else { ?>
                    <strong>1 ngày</strong> sau khi đặt hàng
                <?php } ?>
            </td>
        </tr>  
        <tr>
            <td height="70" style="padding-left:20px; border-bottom:1px dotted #666; border-right:1px dotted #666">Hình thức<br />
                thanh toán:</td>
            <td style="padding-left:5px; border-bottom:1px dotted #666" ><p>Nhận tiền sau khi giao hàng</p></td>
        </tr>

        <td align="justify" colspan="3" style="border-bottom:1px solid #333; padding-bottom:5px; color:#F00">
        </td>
        </tr>  
    </table>
    <table width="560" border="0" cellspacing="0" cellpadding="0" style="padding-top:5px;">
        <tr>
            <td>
                <div id="TabbedPanels1" class="TabbedPanels">
                    <ul class="TabbedPanelsTabGroup">
                        <li class="TabbedPanelsTab" tabindex="0">Mô tả sản phẩm</li>
                    </ul>

                    <div class="TabbedPanelsContentGroup">
                        <!-- Mô tả sản phẩm --> 
                        <div class="TabbedPanelsContent">
                            <table width="752" border="0" cellspacing="0" cellpadding="0">
                                <tr>
                                    <td style="line-height:25px"><?php echo $description; ?></td>
                                </tr>
                            </table>
                        </div>
                        <!-- end - Mô tả sản phẩm -->        

                    </div>
                </div></td>  
        </tr>
    </table>
    <table width="760" border="0" cellspacing="0" cellpadding="0" style="padding-top:5px;">
        <tr>
            <td>
                <div id="TabbedPanels1" class="TabbedPanels">
                    <ul class="TabbedPanelsTabGroup">
                        <li class="TabbedPanelsTab" tabindex="0"><font color="#FFFFFF">Sản phẩm cùng loại</font></li>        
                    </ul>

                    <div class="TabbedPanelsContentGroup">       
                        <!-- sản phẩm cùng loại --> 
                        <div class="TabbedPanelsContent2">
                            <table width="750" border="0" cellspacing="0" cellpadding="0">
                                <tr>
                                    <td align="center" style="padding-left:5px;">    
                                        <?php
                                        foreach ($relatedProducts as $key => $value) {
                                            $price = ($value['gia'] != 0) ? number_format($value['gia'], 0, '', '.') : "(liên hệ)";
                                            if ($value['id'] == $id) {
                                                echo "";
                                            } else {
                                                ?>
                                                <div class=divshow2>
                                                    <table width=175 height=220 border=0 cellspacing=0 cellpadding=0 background="images/box.gif" style="border:1px dotted #999">
                                                        <tr>
                                                            <td height=170><a href=?b=chitiet&id=<?php echo $value['id'] ?>><img src='sanpham/small/<?php echo $value['hinh'] ?>' width=170px height=170 border=0> </a></td>
                                                        </tr>
                                                        <tr>
                                                            <td height=25 style="font-size:14px; color:#F00"><strong><?php echo $value['tensp'] ?></strong></td>
                                                        </tr>
                                                        <tr>
                                                            <td height=25>Giá: <?php echo $price ?></td>
                                                        </tr>
                                                    </table>		
                                                </div>
                                            <?php
                                            }
                                        }
                                        ?>
                                    </td>
                                </tr>
                            </table>
                        </div>
                        <!-- end - sản phẩm cùng loại -->  

                    </div>
                </div></td>  
        </tr>
    </table>
</div>
<script type="text/javascript">
    <!--
var TabbedPanels1 = new Spry.Widget.TabbedPanels("TabbedPanels1");
    //-->
</script>
